package com.service.infocenter.imp;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.mapper.infocenter.MyUploadMapper;
import com.mapper.information.InformationMapper;
import com.po.MyUploadInfo;
import com.service.infocenter.MyUploadService;
import com.util.StringFinal;

@Service
public class MyUploadServiceImp implements MyUploadService {
	@Resource(name = "myUploadMapper")
	private MyUploadMapper myUploadMapper;
	@Resource(name = "informationMapper")
	private InformationMapper informationMapper;

	@Override
	public List<MyUploadInfo> getMyUploadList(MyUploadInfo myUploadInfo) {
		List<MyUploadInfo> list = myUploadMapper.getMyUploadList(myUploadInfo);
		return list;
	}

	@Override
	public int getMyUploadCount(MyUploadInfo myUploadInfo) {
		int i = myUploadMapper.getMyUploadCount(myUploadInfo);
		return i;
	}

	@Override
	public int deleteMyUpload(MyUploadInfo myUploadInfo) {
		int i = myUploadMapper.deleteMyUpload(myUploadInfo);		
		return i;
	}

	@Override
	public int deleteMyUploads(String[] ids,String[] urls,String servletpath) {
			int i=myUploadMapper.deleteMyUploads(ids);
			if(i>0){
				StringBuffer buffer =new StringBuffer();
				 for(String str:urls){
					 File delfile = new File(servletpath+buffer.append(str).toString());
					 if(delfile.exists()){
						 delfile.delete();
						 //删除预览文件
						String pf = str.split("\\.")[1];
						if(pf.contains("doc")||pf.contains("ppt")||pf.contains("xls")){
							 String viewpath = servletpath+StringFinal.viewpath+(str.split("/")[1].split("\\.")[0])+".pdf";
							 StringBuffer viewbuffer = new StringBuffer(viewpath);
							  File delviewfile = new File(viewbuffer.toString());
							 if(delviewfile.exists())
								delviewfile.delete();
							    viewbuffer.setLength(0);
						 }
					 }
					 buffer.setLength(0);	
				 }
			}
		
		 return i;
	}

	@Override
	public int addInfodelete(MyUploadInfo myUploadInfo) {
		// TODO Auto-generated method stub
		int i = myUploadMapper.addInfodelete(myUploadInfo);
		return i;
	}

	@Override
	public int addInfodeleteList(String[] ids, String[] urls, String[] names, String[] kutypes, String userid) {
		// TODO Auto-generated method stub
		ArrayList<MyUploadInfo> list =new ArrayList<MyUploadInfo>();
		for(int i=0;i<ids.length;i++){
			MyUploadInfo info = new MyUploadInfo();
			info.setId(ids[i]);
			info.setName(names[i]);
			info.setKutype(kutypes[i]);
			info.setUser_id(userid);
			list.add(info);
		}
		int i = myUploadMapper.addInfodeletList(list);
		return i;
	}
	
}
